//
//  CZWelcomeViewController.swift
//  wuJunQianWeiBo
//
//  Created by apple on 15/12/19.
//  Copyright © 2015年 wujunqian. All rights reserved.
//

import UIKit

class CZWelcomeViewController: UIViewController {
    
    //头像约束做个全局变量
    private var iconViewCon: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        //调用父类方法
        super.viewDidAppear(animated)
        //头像做动画，修改约束值就可以了
        self.iconViewCon?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        //然后在有弹簧效果的动画方法里更新约束即可
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //更新约束
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //将label渐变出来
                //先让它完全透明
                self.welcomeLabel.alpha = 0
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    //显示label
                    self.welcomeLabel.hidden = false
                })
        }
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 1.添加子控件
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 2.添加约束
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景,填充父控件
        view.addConstraint(NSLayoutConstraint(item: backImageView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: backImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: backImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: backImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 头像,水平和父控件重合
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 垂直方向头像的底部和父控件的底部重合
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        //将已经约束的最后一个约束赋给全局头像约束变量
        iconViewCon = self.view.constraints.last
        
        // 欢迎的label,参照头像
        // 水平方向,欢迎label水平和头像 centerX重合
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 垂直方欢迎label水平和头像距离头像底部16
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }

    
    //MARK: - 懒加载
    //背景
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var iconView: UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //将图片切成圆的
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    //欢迎label
    private lazy var welcomeLabel: UILabel =
    {
        let label = UILabel()
        //设置内容
        label.text = "欢迎回来"
        //设置文字大小
        label.font = UIFont.systemFontOfSize(14)
        label.hidden = true
        return label
    }()

}
